#!/usr/bin/env groovy
package com.yidao.www

def autoBuild(project, branch){
    def date = new Date()
    def embedBuildProfile = embedBuildProfile(project, branch, date.format("yyyyMMdd"))
    println embedBuildProfile
    def label = embedBuildProfile.label
    def gitUrl = embedBuildProfile.gitUrl
    def buildScript = embedBuildProfile.buildScript
    def verFile = embedBuildProfile.verFile
    def artifactDir = embedBuildProfile.artifactDir
    def artifact = embedBuildProfile.artifactName
    def artifactory = embedBuildProfile.artifactory
    def versionInfo = project + '_' + branch + '_' + BUILD_NUMBER

    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def notificationProfile = notificationProfile(project)
    def emailList
    println 'notificationProfile' + notificationProfile
    if(notificationProfile != null){
        emailList = notificationProfile.email
    }
    println 'emailList: ' + emailList
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def storageURL

    node(label){

        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            currentBuild.description = storageURL
            base.gitClone(gitUrl, branch)
        }

        stage('build'){
            try{
                def status = base.batScriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译脚本异常!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        stage('package'){
            if(verFile){
                def ver = "版本号:\n${versionInfo}\n编译日期:\n${date}"
                base.writeFile(ver, verFile)
            }
            try{
                base.zipPackage(artifact, artifactDir)
            }catch(e){
                println '[ERROR] 打包失败!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }

        }

        stage('archive'){
            base.archive(artifact)
        }

        stage('upload'){
            print "artifact: ${artifact}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifact, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('clean'){
            cleanWs()
        }

        stage('retrun artifact'){
            if(emailList != null){
                notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
            }
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }

    }
}


def autoBuildWithMerge(project, branch, bootProject){
    def date = new Date()
    def embedBuildProfile = embedBuildProfile(project, branch, date.format("yyyyMMdd"))
    println embedBuildProfile
    def label = embedBuildProfile.label
    def gitUrl = embedBuildProfile.gitUrl
    def buildScript = embedBuildProfile.buildScript
    def verFile = embedBuildProfile.verFile
    def artifactDir = embedBuildProfile.artifactDir
    def artifact = embedBuildProfile.artifactName
    def artifactory = embedBuildProfile.artifactory
    def versionInfo = project + '_' + branch + '_' + BUILD_NUMBER

    def base = new base()
    def commonScript = new commonScript()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def notificationProfile = notificationProfile(project)
    def emailList
    println 'notificationProfile' + notificationProfile
    if(notificationProfile != null){
        emailList = notificationProfile.email
    }
    println 'emailList: ' + emailList
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def storageURL

    node(label){

        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            currentBuild.description = storageURL
            base.gitClone(gitUrl, branch)
        }

        stage('build'){
            try{
                def status = base.batScriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译脚本异常!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        //如果需要合并 hex 文件，执行
        if(bootProject){
            stage('hexMerge'){
                def bootDir = 'BOOT'
                if(branch == 'M100_MH_BAT_LED_APP'){
//                    此项目无boot，无需合并
                }else base.copyArtifacts(bootProject, bootDir)

                if(project == 'M100Embed'){
                    commonScript.embedHexMergeM100(bootDir, artifactDir)
                }else if(project == 'm100tr4embed' && branch == 'M100_MH_BAT_LED_APP'){
//                    此项目无boot，无需合并
                }else if(project == 'm100tr4embed' && branch != 'M100_MH_BAT_LED_APP'){
                    commonScript.embedHexMergeM100(bootDir, artifactDir)
                }else{
                    commonScript.embedHexMerge(bootDir, artifactDir)
                }
            }
        }

        stage('package'){
            if(verFile){
                def ver = "版本号:\n${versionInfo}\n编译日期:\n${date}"
                base.writeFile(ver, verFile)
            }
            try{
                base.zipPackage(artifact, artifactDir)
            }catch(e){
                println '[ERROR] 打包失败!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }

        }

        stage('archive'){
            base.archive(artifact)
        }

        stage('upload'){
            print "artifact: ${artifact}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifact, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('clean'){
            cleanWs()
        }

        stage('retrun artifact'){
            if(emailList != null){
                notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
            }
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }

    }
}




def autoBuildWithMerge3in1(project, branch, merge){
    def date = new Date()
    def embedBuildProfile = embedBuildProfile(project, branch, date.format("yyyyMMdd"))
    println embedBuildProfile
    def label = embedBuildProfile.label
    def gitUrl = embedBuildProfile.gitUrl
    def buildScript = embedBuildProfile.buildScript
    def verFile = embedBuildProfile.verFile
    def artifactDir = embedBuildProfile.artifactDir
    def artifact = embedBuildProfile.artifactName
    def artifactory = embedBuildProfile.artifactory
    def versionInfo = project + '_' + branch + '_' + BUILD_NUMBER

    def base = new base()
    def commonScript = new commonScript()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def notificationProfile = notificationProfile(project)
    def emailList
    println 'notificationProfile' + notificationProfile
    if(notificationProfile != null){
        emailList = notificationProfile.email
    }
    println 'emailList: ' + emailList
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def storageURL
    def ProcessFile= "ProcessFile.bat"

    node(label){

        stage('clean'){
            cleanWs()
        }

        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            currentBuild.description = storageURL
            base.gitClone(gitUrl, branch)
        }

        stage('build'){
            try{
                def status = base.batScriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译脚本异常!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        //如果需要合并 hex 文件，执行
        if(merge == true){
            stage('hexMerge'){
                commonScript.embedHexMerge3in1()
            }
        }else if(merge == 4){
            stage('hexMerge'){
                commonScript.embedHexMerge4in1(project)
            }
        }else if(merge == 'standard'){
            stage('hexMerge'){
                commonScript.embedHexMergeStandard(project)
            }
        }else if(merge == '3standard'){
            stage('hexMerge'){
                commonScript.embedHexMerge3in1Standard(project)
            }
        }

        if(project == 'A200Eembed') {
            stage('ProcessFile') {
                try {
                    println 'ProcessFile开始处理文件！'
                    def status = base.batScriptStatus(ProcessFile)
                    if (status != 0) {
                        currentBuild.result = 'FAILURE'
                        error '[ERROR]  ProcessFile 失败!'
                    } else println 'ProcessFile  处理文件成功!'

                } catch (e) {
                    println '[ERROR]  ProcessFile 脚本异常!'
                    currentBuild.result = 'FAILURE'
                    error e.getMessage()
                }
            }
        }


        stage('package'){
            if(verFile){
                def ver = "版本号:\n${versionInfo}\n编译日期:\n${date}"
                base.writeFile(ver, verFile)
            }
            try{
                base.zipPackage(artifact, artifactDir)
            }catch(e){
                println '[ERROR] 打包失败!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }

        }

        stage('archive'){
            base.archive(artifact)
        }

        stage('upload'){
            print "artifact: ${artifact}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifact, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('clean'){
            cleanWs()
        }

        stage('retrun artifact'){
            if(emailList != null){
                notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
            }
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }

    }
}

def autoBuildWithMergeZ200(project, branch, merge){
    def date = new Date()
    def embedBuildProfile = embedBuildProfile(project, branch, date.format("yyyyMMdd"))
    println embedBuildProfile
    def label = embedBuildProfile.label
    def gitUrl = embedBuildProfile.gitUrl
    def buildScript = embedBuildProfile.buildScript
    def verFile = embedBuildProfile.verFile
    def artifactDir = embedBuildProfile.artifactDir
    def artifact = embedBuildProfile.artifactName
    def artifactory = embedBuildProfile.artifactory
    def versionInfo = project + '_' + branch + '_' + BUILD_NUMBER

    def base = new base()
    def commonScript = new commonScript()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    def notificationProfile = notificationProfile(project)
    def emailList
    println 'notificationProfile' + notificationProfile
    if(notificationProfile != null){
        emailList = notificationProfile.email
    }
    println 'emailList: ' + emailList
    def notification = new notification()
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def storageURL
    def ProcessFile= "ProcessFile.bat"

    node(label){

        stage('clean'){
            cleanWs()
        }

        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            currentBuild.description = storageURL
            base.gitClone(gitUrl, branch)
        }

        stage('build'){
            try{
                def status = base.batScriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译脚本异常!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        //如果需要合并 hex 文件，执行
        if(merge == true){
            stage('hexMerge'){
                commonScript.embedHexMerge3in1()
            }
        }else if(merge == 4){
            stage('hexMerge'){
                commonScript.embedHexMerge4in1(project)
            }
        }else if(merge == 'standard'){
            stage('hexMerge'){
                commonScript.embedHexMergeStandard(project)
            }
        }else if(merge == '3standard'){
            stage('hexMerge'){
                commonScript.embedHexMerge3in1Standard(project)
            }
        }else if(merge == 'embedHexMergeStandard2'){
            stage('hexMerge'){
                commonScript.embedHexMergeStandard2(project)
            }
        }

        stage('ProcessFile'){
            try{
                def status = base.batScriptStatus(ProcessFile)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    error '[ERROR]  ProcessFile 失败!'
                }else println 'ProcessFile  处理文件成功!'

            }catch(e){
                println '[ERROR]  ProcessFile 脚本异常!'
                currentBuild.result = 'FAILURE'
                error e.getMessage()
            }
        }


        stage('package'){
            if(verFile){
                def ver = "版本号:\n${versionInfo}\n编译日期:\n${date}"
                base.writeFile(ver, verFile)
            }
            try{
                base.zipPackage(artifact, artifactDir)
            }catch(e){
                println '[ERROR] 打包失败!'
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }

        }

        stage('archive'){
            base.archive(artifact)
        }

        stage('upload'){
            print "artifact: ${artifact}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifact, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                currentBuild.result = 'FAILURE'
                base.influxDb()
                if(emailList != null){
                    notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
                }
                error e.getMessage()
            }
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('clean'){
            cleanWs()
        }

        stage('retrun artifact'){
            if(emailList != null){
                notification.emailNotification(jobName, buildNumber, emailList, currentBuild.result)
            }
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }

    }
}